//package com.itheima.a39;
//
//import org.springframework.beans.factory.support.DefaultListableBeanFactory;
//import org.springframework.beans.factory.xml.XmlBeanDefinitionReader;
//import org.springframework.boot.*;
//import org.springframework.boot.web.embedded.tomcat.TomcatServletWebServerFactory;
//import org.springframework.boot.web.reactive.context.AnnotationConfigReactiveWebServerApplicationContext;
//import org.springframework.boot.web.servlet.context.AnnotationConfigServletWebServerApplicationContext;
//import org.springframework.boot.web.servlet.server.ServletWebServerFactory;
//import org.springframework.context.ApplicationContextInitializer;
//import org.springframework.context.ConfigurableApplicationContext;
//import org.springframework.context.annotation.*;
//import org.springframework.context.support.GenericApplicationContext;
//import org.springframework.core.io.ClassPathResource;
//import org.springframework.test.context.junit4.SpringRunner;
//
//import java.util.Arrays;
//import java.util.Set;
//
//// 运行时请添加运行参数 --server.port=8080 debug
//public class A39_3 {
//    @SuppressWarnings("all")
//    public static void main(String[] args) throws Exception {
//        SpringApplication app = new SpringApplication();
//        app.addInitializers(new ApplicationContextInitializer<ConfigurableApplicationContext>() {
//            @Override
//            public void initialize(ConfigurableApplicationContext applicationContext) {
//                System.out.println("执行初始化器增强...");
//            }
//        });
//
//        System.out.println(">>>>>>>>>>>>>>>>>>>>>>>> 2. 封装启动 args");
//        DefaultApplicationArguments arguments = new DefaultApplicationArguments(args);
//
//        System.out.println(">>>>>>>>>>>>>>>>>>>>>>>> 8. 创建容器");
//        GenericApplicationContext context = createApplicationContext(WebApplicationType.SERVLET);
//
//        System.out.println(">>>>>>>>>>>>>>>>>>>>>>>> 9. 准备容器");
//        for (ApplicationContextInitializer initializer : app.getInitializers()) {
//            initializer.initialize(context);
//        }
//
//        System.out.println(">>>>>>>>>>>>>>>>>>>>>>>> 10. 加载 bean 定义");
//        DefaultListableBeanFactory beanFactory = context.getDefaultListableBeanFactory();
//        AnnotatedBeanDefinitionReader reader1 = new AnnotatedBeanDefinitionReader(beanFactory);
//        XmlBeanDefinitionReader reader2 = new XmlBeanDefinitionReader(beanFactory);
//        ClassPathBeanDefinitionScanner scanner = new ClassPathBeanDefinitionScanner(beanFactory);
//
//        reader1.register(Config.class);
//        reader2.loadBeanDefinitions(new ClassPathResource("b03.xml"));
//        scanner.scan("com.itheima.a39.sub");
//
//        System.out.println(">>>>>>>>>>>>>>>>>>>>>>>> 11. refresh 容器");
//        context.refresh();
//
//        for (String name : context.getBeanDefinitionNames()) {
//            System.out.println("name:" + name + " 来源：" + beanFactory.getBeanDefinition(name).getResourceDescription());
//        }
//
//        System.out.println(">>>>>>>>>>>>>>>>>>>>>>>> 12. 执行 runner");
//        for (CommandLineRunner runner : context.getBeansOfType(CommandLineRunner.class).values()) {
//            runner.run(args);
//        }
//
//        for (ApplicationRunner runner : context.getBeansOfType(ApplicationRunner.class).values()) {
//            runner.run(arguments);
//        }
//
//        /*
//            学到了什么
//            a. 创建容器、加载 bean 定义、refresh, 对应的步骤
//
//         */
//    }
//
//    private static GenericApplicationContext createApplicationContext(WebApplicationType type) {
//        GenericApplicationContext context = null;
//        switch (type) {
//            case SERVLET -> context = new AnnotationConfigServletWebServerApplicationContext();
//            case REACTIVE -> context = new AnnotationConfigReactiveWebServerApplicationContext();
//            case NONE -> context = new AnnotationConfigApplicationContext();
//        }
//        return context;
//    }
//
//    static class Bean4 {
//
//    }
//
//    static class Bean5 {
//
//    }
//
//    static class Bean6 {
//
//    }
//
//    @Configuration
//    static class Config {
//        @Bean
//        public Bean5 bean5() {
//            return new Bean5();
//        }
//
//        @Bean
//        public ServletWebServerFactory servletWebServerFactory() {
//            return new TomcatServletWebServerFactory();
//        }
//
//        @Bean
//        public CommandLineRunner commandLineRunner() {
//            return new CommandLineRunner() {
//                @Override
//                public void run(String... args) throws Exception {
//                    System.out.println("commandLineRunner()..." + Arrays.toString(args));
//                }
//            };
//        }
//
//        @Bean
//        public ApplicationRunner applicationRunner() {
//            return new ApplicationRunner() {
//                @Override
//                public void run(ApplicationArguments args) throws Exception {
//                    System.out.println("applicationRunner()..." + Arrays.toString(args.getSourceArgs()));
//                    System.out.println(args.getOptionNames());
//                    System.out.println(args.getOptionValues("server.port"));
//                    System.out.println(args.getNonOptionArgs());
//                }
//            };
//        }
//    }
//}
